package com.common.www;

import com.common.www.service.auth.UserService;
import org.springframework.boot.base.jpa.SpringBootApplicationExtension;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.TransactionManagementConfigurer;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.persistence.EntityManagerFactory;

@RestController
@SpringBootApplication
@EnableScheduling
@SpringBootApplicationExtension
@EnableTransactionManagement
@EnableCaching
@ServletComponentScan(basePackages = {"com.yidian.www.filter"})
public class MyApplication extends SpringBootServletInitializer implements TransactionManagementConfigurer {
    @Resource(name = "transactionManager")
    private PlatformTransactionManager txManager;

//    // 创建事务管理器1
//    @Bean(name = "transactionManager")
//    public PlatformTransactionManager txManager2(DataSource dataSource) {
//        return new DataSourceTransactionManager(dataSource);
//    }

    // 创建事务管理器2
    @Bean(name = "transactionManager")
    public PlatformTransactionManager txManager(EntityManagerFactory factory) {
        return new JpaTransactionManager(factory);
    }

    // 实现接口 TransactionManagementConfigurer 方法，其返回值代表在拥有多个事务管理器的情况下默认使用的事务管理器
    @Override
    public PlatformTransactionManager annotationDrivenTransactionManager() {
        return txManager;
    }

    /**
     * 打印当前使用的事务管理器的名称
     */
//    @Bean
//    public Object printTransactionName(PlatformTransactionManager platformTransactionManager) {
//        System.out.println(">>>>>>>>>>=========================================" + platformTransactionManager.getClass().getName());
//        return new Object();
//    }

    @Autowired
    private UserService userService;

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(this.getClass());
    }


    @PostConstruct
    void setDefaultTimezone() {

    }

    public static void main(String[] args) {
//        SpringApplication.run(MyApplication.class, args);
        SpringApplicationBuilder builder = new SpringApplicationBuilder(MyApplication.class);
        builder.headless(false)
                // .web(WebApplicationType.NONE)
                // .bannerMode(Banner.Mode.OFF)
                .run(args);
        System.setProperty("tomcat.util.http.parser.HttpParser.requestTargetAllow", "[]|{}");


    }

}
